<template>
  <div class="loginbox">
    <h1>登入验证</h1>
    <div class="formbox">
      <el-form
        ref="ruleForm"
        :model="formData"
        class="demo-ruleForm"
        :rules="rules"
      >
        <el-form-item prop="username">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formData.password"></el-input>
        </el-form-item>
        <el-button type="primary" class="btn" @click.stop="loginhandle"
          >登入</el-button
        >
      </el-form>
    </div>
    <div class="tishi">
      <p class="text">温馨提示</p>
      <p class="text">未登录过的新用户，自动注册</p>
      <p class="text">注册过的用户可凭账号密码登录</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        username: "",
        password: "",
      },
      rules: {
        username: {
          required: true,
          message: "输入不能为空",
          trigger: "blur",
        },
        password: {
          required: true,
          message: "输入不能为空",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    loginhandle() {
      console.log("登入");
      this.$axios
        .post("/admin/login", {
          user_name: this.formData.username,
          password: this.formData.password,
        })
        .then((res) => {
          console.log(res);
          if (res.status === 1) {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            /* 登录成功进入home路由 */
            this.$router.push("home");
          } else {
            // this.$message({
            //   type: "error",
            //   message: "登录错误！请检查网络或者账号密码！",
            // });
            this.$notify({
              title: "账号密码验证不通过！",
              message: "账号或者密码有误！",
              type: "warning",
              showClose: false,
            });
          }
        });
    },
  },
};
</script>

<style lang="less">
div.loginbox {
  padding: 15px 0;
  width: 400px;
  margin: 30px auto;
  background-color: #fff;
  border-radius: 10px;
  h1 {
    font-size: 26px;
    line-height: 40px;
  }

  div.formbox {
    text-align: center;
    .demo-ruleForm {
      margin: 0 auto;
      width: 90%;
      .btn {
        width: 100%;
        margin: 10px 0;
      }
    }
  }
  .tishi {
    text-align: center;
    p {
      line-height: 30px;
      color: red;
    }
  }
}
</style>
